package com.example.demo1.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.demo1.R;

import java.util.List;

public class CustomAdapter extends BaseAdapter {


    private LayoutInflater inflater;

    private List<String> list;

    public CustomAdapter(List<String> list, Context context) {
        //实例化界面
        inflater = LayoutInflater.from(context);

        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        if (i >= 0 && i < list.size()) {
            return list.get(i);
        }
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        //实例化界面
        view = inflater.inflate(R.layout.gridview_item, null);

        //界面中找到text控件
        TextView textView = view.findViewById(R.id.textview);

        String s = list.get(i);

        //给text控件设置值
        textView.setText(s);

        return view;
    }
}
